import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

public class FileIndexer {
	String root = null;
	String dataPath = null;
	public FileIndexer(String rootPath, String dataPath)
	{
		this.root = rootPath;
		this.dataPath = dataPath;

		File folder = new File(dataPath);
		if(!folder.exists())
		{
			if(!folder.mkdirs())
			{
				System.err.println("Can not create folder:" + dataPath);
				System.exit(0);
			}
			
		}
	}
	protected void indexFiles(String lsFileName)
	{
		System.out.println(lsFileName);
		
		File currentFile = new File(root + lsFileName.substring(1));
		File lsFile = new File(dataPath + lsFileName.replace('/', '~'));
		try
		{
			FileOutputStream out=new FileOutputStream(lsFile);
			PrintStream p=new PrintStream(out);
			if(currentFile.isDirectory())
			{
				File files[] = currentFile.listFiles();
				for(File f:files)
				{
					if(f.isDirectory())
					{
						//do not look at
						if(!f.getName().equals(".LS-INFO"))
							p.println(f.getName() + "\n1");
					}
					else
						p.println(f.getName() + "\n2");
				}
				p.close();
				for(File f:files)
				{
					//do not look in
					if(f.isDirectory() && (!f.getName().equals(".LS-INFO")))
						indexFiles(lsFileName + f.getName() + "/");
				}
			}
		}catch (FileNotFoundException e){
			e.printStackTrace();
		}
	}
}
